package com.rowline.admin.module.util.utils;

//import org.apache.commons.lang.time.DateFormatUtils;
//import org.joda.time.DateTime;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.joda.time.DateTime;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * 时间工具
 * 封装大量工具方法
 * @author 198777
 *
 */
public class DateTimeUtil {

	public static final String DEFAULT_DATETIME_FORMAT_SEC = "yyyy-MM-dd HH:mm:ss";

	public static final String ISO_DATE_FORMAT = "yyyy-MM-dd";

	public static final String ISO_DATE_FORMAT_HH = "yyyy-MM-dd HH";

	public static final String ISO_DATE_FORMAT_MM = "yyyy-MM";

	public static final String ISO_DATETIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";

	private static DatatypeFactory datatypeFactory;
     //单利
	static {
		try {
			datatypeFactory = DatatypeFactory.newInstance();
		} catch (DatatypeConfigurationException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @author 李鹏飞
	 * @Description 将XMLGregorianCalendar转换为Date
	 * @Time 2013-5-16
	 * @param cal
	 * @return
	 */
	public static Date convertToDate(XMLGregorianCalendar cal) {
		if (cal == null) {
			return null;
		} else {
			GregorianCalendar ca = cal.toGregorianCalendar();
			return ca.getTime();
		}
	}

	/**
	 * @author 杨长燕
	 * @Description Date转换为
	 * @Time 2013-5-21
	 * @param date
	 * @return 返回XMLGregorianCalendar类型
	 */
	public static XMLGregorianCalendar convertToXMLDate(Date date) {
		XMLGregorianCalendar gc = null;
		if (date != null) {
			GregorianCalendar cal = new GregorianCalendar();
			cal.setTime(date);
			try {
				gc = datatypeFactory.newXMLGregorianCalendar(cal);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return gc;
	}

	/**
	 * @author 李鹏飞
	 * @Description 获取当前时间前一天
	 * @Time 2013-5-30
	 * @param date
	 * @return
	 */
	public static Date getYesterday(Date date) {
		Date tmpdate = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		/** BI需求待修正点 */
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		tmpdate = calendar.getTime();
		return tmpdate;
	}

	/**
	 * 获取第二天时间
	 */
	public static Date getFollowingDay(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * @author 李鹏飞
	 * @Description 获取本月天数
	 * @Time 2014-8-6
	 * @param date
	 * @return
	 */
	public static int getTotalDayOfMonth(Date date) {
		DateTime dateTime = new DateTime(date);
		Calendar calendar = dateTime.toCalendar(new Locale("zh", "ZH"));
		return calendar.getActualMaximum(Calendar.DATE);
	}

	public static String format(Date date) {
		return format(date, DEFAULT_DATETIME_FORMAT_SEC);
	}

	public static String format(Date date, String pattern) {
		if (null == date) {
			return "";
		}
		return DateFormatUtils.format(date, pattern);
	}

	public static boolean isDate(String date, String pattern) {
		DateFormat df = new SimpleDateFormat(pattern);
		try {
			df.parse(date);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	// 取到字符型时间值
	public static String formatToISO(Date date) {
		if (null == date) {
			return "";
		}
		return DateFormatUtils.format(date, ISO_DATETIME_FORMAT);
	}
	
	// 取到秒值
	public static Date formatToDate(String date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date date1 = sdf.parse(date);
			Date date2 = sdf.parse(sdf.format(date1));
			return date2;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	// 取到毫秒值
	public static Date formatToTime(String date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		try {
			Date date1 = sdf.parse(date);
			Date date2 = sdf.parse(sdf.format(date1));
			return date2;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 本服务内取事件时间戳-到毫秒
	 * @author 219402
	 * @date 2016-7-25
	 * @return String
	 */
	public static String takeTimeStamp() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		String time = " [" + sdf.format(new Date()) + "] ";
		return time;
	}
	
	public static void main(String[] args) {
		System.out.println(System.currentTimeMillis());
	}

	/**
	 * 解析日期时间字符串(无分隔符)为Date类型
	 * @author 378281
	 * @param datetimeStr
	 * @return
	 */
	public static Date parseDatetimeStr(String datetimeStr) {
		Date date = null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		try {
			date = simpleDateFormat.parse(datetimeStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

}
